//
//  RecordViewController.m
//  NewChat
//
//  Created by songKX on 16/3/3.
//  Copyright © 2016年 蓝鸥. All rights reserved.
//

#import "RecordViewController.h"

#import "RecordProgress.h"

#define RECORD [SKXFriendManager sharaFriendManager]

@interface RecordViewController ()

@property (nonatomic , strong)RecordProgress *pro;

@property (nonatomic , strong)NSTimer *timer;
@property (weak, nonatomic) IBOutlet UILabel *recordPro;
@property (nonatomic , assign) BOOL status;
@property (nonatomic , strong) NSString *voicePath;
@end

@implementation RecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.status = NO;
    
    self.pro = [[RecordProgress alloc] initWithFrame:CGRectMake(100, 100, [UIScreen mainScreen].bounds.size.width * 0.5, [UIScreen mainScreen].bounds.size.width * 0.5)];
    
    self.pro.totalTime = 15;
    
    _pro.center = CGPointMake([UIScreen mainScreen].bounds.size.width * 0.5, [UIScreen mainScreen].bounds.size.height * 0.35);
    
    
    [self.view addSubview:_pro];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(saveAction:)];
    
    // Do any additional setup after loading the view.
}
//添加定时器
-(void)addTimer{
    
    self.timer = [NSTimer timerWithTimeInterval:0.05 target:self selector:@selector(startAction) userInfo:nil repeats:YES];
    
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    
}
//取消定时器
-(void)cancelTimer{
    
    [self.timer invalidate];
    self.timer = nil;
    
}
//定时器方法

-(void)startAction{
    
    
    self.pro.progress += 0.05;
    
    if (self.pro.progress >= 15) {
        
        [self cancelTimer];
    }
    
}


//开始录制
- (IBAction)recordStart:(UIButton *)sender {
    
    if ([self.recordPro.text isEqualToString:@"点击录音"]) {
        
        NSLog(@"打开定时器");
        [self addTimer];
        
        [RECORD startRecordVoiceWithFileName:nil];
        
        self.recordPro.text = @"正在录音";
        
    }else if ([self.recordPro.text isEqualToString:@"正在录音"]){
        
        [self cancelTimer];
        
        self.recordPro.text = @"录音结束";
        
        [RECORD stopRecordVoiceWithBlock:^(NSString *path) {
            self.voicePath = path;
        }];
        
    }
    
}
//保存个性签名

-(void)saveAction:(UIBarButtonItem *)sender{
    
    
    NSLog(@"文件地址%@",self.voicePath);
    
    AVFile *file = [AVFile fileWithName:nil contentsAtPath: self.voicePath];
    

    
    NSData *data = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:self.voicePath ]];
    
    if (data == nil) {
        
        [self.navigationController popViewControllerAnimated:YES];

        
    }else{
    
    [file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        
        NSLog(@"%@",file.url);
        
        if (succeeded) {
            
            
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            
            [user setObject:file.url forKey:@"myVoice"];
            [self.navigationController popViewControllerAnimated:YES];
            
        }
    }];

    }
}

@end
